<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//限行
class XianXing extends MY_Controller {

	function __construct(){
		
		parent::__construct();
		
	}

	//start
	/**
	* @name 添加出行记录
	* @param date false char 年-月-日，不传则默认当天
	* @return 
	*/
	//end
	function add_chuxing(){
		$user_id = $this->user_id;
		
		$date = $this->input->post('date',null,'');
		
		
		$this->load->dmodel('m_chepai','chepai');
		
		$this->load->model('User/UserModel','UserModel');
		$chepai = $this->UserModel->get_chepai($user_id);
		
		
		$time = time();
		if(empty($date)){
			$date = date('Y-m-d');
		}
		
		if($date>date('Y-m-d')){
			$this->err_out('不允许添加未到的日期');
		}
		
		$month = date('Y-m',strtotime($date));
		
		$this->load->dmodel('m_chuxing','chuxing');
		
		$is_exits = $this->chuxing->where_exist(array('user_id'=>$user_id,'chepai'=>$chepai,'date'=>$date));
		if($is_exits){
			$this->err_out('该车牌今天已有出行记录');
		}
		
		
		
		$arr = array(
			'user_id'=>$user_id,
			'chepai'=>$chepai,
			'date'=>$date,
			'addtime'=>$time,
			'month'=>$month
		);
		
		$this->chuxing->add($arr);
		
		$this->data_out($arr);
	}
	
	//start
	/**
	* @name 获取出行记录
	* @param year false char 年
	* @param month false char 月
	* @return 
	*/
	//end
	function get_chuxing(){
		$user_id = $this->user_id;
		
		
		$year = $this->input->post('year',null,date('Y'));
		$month = $this->input->post('month',null,date('m'));

		$ymd = $year.'-'.$month.'-01';
		
		$time = strtotime($ymd);
		
		
		$this->load->model('User/UserModel','UserModel');
		$chepai = $this->UserModel->get_chepai($user_id);
		
		
		
		$this->load->dmodel('m_chuxing','chuxing');
		$ymonth = date('Y-m',$time);
		$now_months = $this->chuxing->getAll("user_id='{$user_id}' AND chepai='{$chepai}' AND month='{$ymonth}'");
		
		$prev_ymonth = date('Y-m',strtotime('-1 month',$time));
		$prev_months = $this->chuxing->getAll("user_id='{$user_id}' AND chepai='{$chepai}' AND month='{$prev_ymonth}'");

		
		$arr = array();
		$chuxings = array();
		foreach($prev_months as $val){
			$arr[] = $val['date'];
		}
		

		foreach($now_months as $val){
			$arr[] = $val['date'];
			$chuxings[] = $val['date'];
		}
		asort($arr);
		
		$this->load->library('Kaiting');
		$ls_ymd = date('Y-m-d');
		$days = $this->kaiting->init($arr,$ls_ymd);
		
		$is_guangzhou = strpos($chepai,'粤A');
		
		$data = array(
			'chuxings'=>$chuxings,
			'days'=>$days,
			'is_guangzhou'=>$is_guangzhou!==false,
			'chepai'=>$chepai
		);
		$this->data_out($data);
	}
	//start
	/**
	* @name 删除出行记录
	* @param date false char 年-月-日
	* @return 
	*/
	//end
	function del(){
		$user_id = $this->user_id;
		$date = $this->input->post('date',null,'');
		
		
		$this->load->model('User/UserModel','UserModel');
		$chepai = $this->UserModel->get_chepai($user_id);
		
		$this->load->dmodel('m_chuxing','chuxing');
		
		$is_exits = $this->chuxing->where_exist(array('user_id'=>$user_id,'chepai'=>$chepai,'date'=>$date));
		if(!$is_exits){
			$this->err_out('该车牌当天还没有出行记录');
		}
		
		$this->chuxing->del("user_id='{$user_id}' AND chepai='{$chepai}' AND date='{$date}'");
		$this->data_out(1);
		
	}
}
